# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python
import shutil
import os
from cerbero.errors import FatalError
from cerbero.utils.shell import which

class Recipe(recipe.Recipe):
    name = 'glib-networking-static'
    version = '2.48.2'
    licenses = [License.LGPLv2Plus]
    stype = SourceType.TARBALL
    url = 'http://ftp.gnome.org/pub/gnome/sources/glib-networking/2.48/glib-networking-%(version)s.tar.xz'
    tarball_dirname = 'glib-networking-%(version)s'
    autoreconf = True
    configure_options = "--without-ca-certificates --enable-static-modules --enable-static --enable-more-warnings"
    deps = ['glib', 'gnutls', 'glib-networking']
    patches = ['glib-networking/0001-Add-support-for-static-modules.patch',
               'glib-networking/0002-Get-the-CA-certificate-path-from-the-environment-var.patch',
               'glib-networking/0003-gnutls-Use-db-relative-to-libglib-2.0-if-needed.patch',
               'glib-networking/0004-gtlsbackend-gnutls-Get-anchor-file-relative-to-libgi.patch']


    files_devel = ['lib/gio/modules/static/libgiognutls.a',
                   'lib/gio/modules/static/libgiognutls.la']

    def prepare(self):
        self.tmp_destdir = os.path.join(self.build_dir, 'static-build')

        querymodule_path = which('true')

        if self.config.target_platform in [Platform.ANDROID, Platform.IOS]:
          self.append_env['CFLAGS'] = ' -DGST_CA_CERTIFICATES_FROM_ENV'

        self.config_sh = 'GIO_QUERYMODULES=%s %s' % (querymodule_path, self.config_sh)

    def install(self):
        plugins_dir = os.path.join(self.config.prefix,
                                    os.path.dirname(self.files_devel[0]))
        if not os.path.exists(plugins_dir):
            os.makedirs(plugins_dir)
        shutil.copy(os.path.join(self.build_dir, 'tls', 'gnutls', '.libs',
            'libgiognutls.a'), plugins_dir)
        shutil.copy(os.path.join(self.build_dir, 'tls', 'gnutls', '.libs',
            'libgiognutls.la'), plugins_dir)
